/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SISAC.DACEA.DA;

import SISAC.DACEA.DS.Conexion;
import SISAC.DACEA.ENTITY.Cobro;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;

/**
 *
 * @author Abel
 */
public class CobroDA {
    public synchronized void crearCobro(Cobro oCobro){
        CallableStatement call = null;
        Connection cn=null;
        try {
            String sql="{CALL insertarCobro(?,?,?,?) }";
            
            cn = new Conexion().getConnection();
            call=cn.prepareCall(sql);
            
            call.registerOutParameter(1, Types.INTEGER);
            call.setDouble(2, oCobro.getCuotaUno());
            call.setDouble(3, oCobro.getCuotaDos());
            call.setDouble(4, oCobro.getCuotaTres());
            
             call.executeUpdate();
            //recuperamos el codigoCobro
            
            oCobro.setCodigoCobro(call.getInt(1));
            
        } catch (Exception e) {
            System.out.println("Error en insertar cobro "+e);
        }
    
    }
    public synchronized ArrayList<Cobro> obtenerCuotas(Cobro oCobro){
        Connection cn = null;
        CallableStatement call = null;
        ResultSet rs = null;
        ArrayList<Cobro> lstCuotas = new ArrayList<Cobro>();
        try {
            
            String sql = "{CALL obtenerCuotas(?)}";

            cn = new Conexion().getConnection();
            call = cn.prepareCall(sql);
            call.setInt(1, oCobro.getCodigoCobro());
            rs = call.executeQuery();
            
            while(rs.next()){
                
                Cobro oCobro1 = new Cobro();
                oCobro1.setCodigoCobro(rs.getInt(1));
                oCobro1.setCuotaUno(rs.getDouble(2));
                oCobro1.setCuotaDos(rs.getDouble(3));
                oCobro1.setCuotaTres(rs.getDouble(4));
                
                lstCuotas.add(oCobro1);
            }
            return lstCuotas;
        } catch (Exception e) {
            System.out.println("error al obtern cobro "+e);
            return null;
        }
        
    }
    
}
